% Copyright 2003--2007 by Till Tantau
% Copyright 2010 by Vedran Mileti\'c
% Copyright 2012,2015 by Vedran Mileti\'c, Joseph Wright
% Copyright 2017,2018 by Louis Stuart, Joseph Wright
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/licenses/LICENSE for more details.

\mode
<presentation>

\newbox\beamer@frameboxcopy
\newif\ifbeamer@twoscreensnotes


% Notes options

\defbeameroption{hide notes}[]%
{
  \beamer@notesfalse
}

\defbeameroption{show notes}[]%
{
  \beamer@notestrue
}

\defbeameroption{show notes on second screen}[right]%
{
  \ifx\@onlypreamble\@notprerr 
    \ltx@ifpackageloaded{pgfpages}{}{%
      \ClassError{beamer}{`pgfpages' package necessary for this option}{}%
    }%
  \else%
    \RequirePackage{pgfpages}%
  \fi%
  \pgfpagesuselayout{two screens with optional second}[second #1]
  \beamer@twoscreensnotestrue
  \beamer@notestrue
}

\defbeameroption{show only notes}[]%
{
  \beamer@notestrue
  \beamer@notesnormalsfalse
  \nofiles
}

\defbeameroption{show only slides with notes}[]%
{
  \beamer@notestrue
  \beamer@frameswithnotesonlytrue
  \nofiles
}

%
% Notes
%

\define@key{beamernotes}{enumerate}[true]{%
  \def\beamer@noteenvstart{\begin{enumerate}\itemsep=0pt\parskip=0pt}%
  \def\beamer@noteenvend{\end{enumerate}}}
\define@key{beamernotes}{itemize}[true]{%
  \def\beamer@noteenvstart{\begin{itemize}\itemsep=0pt\parskip=0pt}%
  \def\beamer@noteenvend{\end{itemize}}}

\newcommand\beamer@outsideframenote[2][]{%
  \beamer@savemode%
  \ifbeamer@inlecture%
    \def\beamer@noteenvstart{}%
    \def\beamer@noteenvend{}%
    \setkeys{beamernotes}{#1}%
    \ifbeamer@notes
    \begingroup
      \setbeamertemplate{itemize item}{\textbullet}
      \setbeamertemplate{itemize subitem}{--}
      \setbeamertemplate{enumerate item}{\insertenumlabel.}
      \setbeamertemplate{enumerate subitem}{\insertenumlabel.\insertsubenumlabel}
      \def\@oddhead{}
      \def\@oddfoot{}
      \let\@evenhead\@oddhead
      \let\@evenfoot\@oddfoot
      \def\beamer@backgroundtemplate{}%
      \setbeamercolor{item}{fg=black,bg=white}
      \color{black}%
      \nointerlineskip
      \hbox{\hskip-\Gm@lmargin\hskip1cm\vbox to\textheight{%
                                  %pretend to have ``standard'' margins
          \edef\beamer@origlmargin{\Gm@lmargin}%
          \edef\beamer@origrmargin{\Gm@rmargin}%
          \def\Gm@lmargin{1cm}%
          \def\Gm@rmargin{1cm}%
          \textwidth=\dimexpr\paperwidth-\Gm@lmargin-\Gm@rmargin\relax%
          \hsize=\textwidth%
          \@arrayparboxrestore%
          \vskip-\headheight%
          \def\insertnote{\vbox{}%
            \beamer@noteenvstart#2\beamer@noteenvend%
          }%
          \usebeamertemplate*{note page}%
          \vfil%
          \vskip-4pt% foot separator
          \vskip-\footheight}\hskip-\Gm@lmargin\hskip1cm}%
      \ifbeamer@twoscreensnotes%
        \pgfpagescurrentpagewillbelogicalpage{2}%
        \advance\c@page by-1\relax%
      \fi%
      \clearpage
      \endgroup
    \fi%
  \fi%
  \beamer@resumemode}

%
% Notes in frames
%

\def\beamer@itemtext{item}

\newcommand<>{\beamer@inframenote}[2][]{%
  \ifbeamer@inlecture%
    \only#3{%
      \def\beamer@temp{#1}%
      \ifx\beamer@temp\beamer@itemtext%
        \expandafter\gdef\expandafter\beamer@noteitems%
        \expandafter{\beamer@noteitems\item#2}%
      \else
        \expandafter\gdef\expandafter\beamer@notes%
          \expandafter{\beamer@notes#2}%
      \fi%
    }%
  \fi%
  }

\def\note{%
  \ifbeamer@inframe%
    \let\next=\beamer@inframenote%
  \else%
    \let\next=\beamer@outsideframenote%
  \fi%
  \next}

\def\beamer@framenotesbegin{% at beginning of slide
  \gdef\beamer@noteitems{}%
  \gdef\beamer@notes{}%
}

\def\beamer@framenotesend
{% at end of slide
  \global\setbox\beamer@frameboxcopy
    =\hbox{\leaders\copy\beamer@framebox\hskip\wd\beamer@framebox}
  \ifx\beamer@noteitems\@empty%
    \ifx\beamer@notes\@empty%
      % possibly suppress this slide
      \ifbeamer@frameswithnotesonly
        % suppress frame
        \global\setbox\beamer@framebox=\box\voidb@x%
      \fi%
      \ifbeamer@twoscreensnotes%
        \beamer@setupnote%
      \fi%
    \else%
      \beamer@setupnote%
    \fi
  \else%
    \beamer@setupnote%
  \fi
  \ifbeamer@notesnormals%
  \else%
    \global\setbox\beamer@framebox=\box\voidb@x%
  \fi%
}

\def\beamer@setupnote{%
  \gdef\beamer@notesactions{%
    \beamer@outsideframenote{%
      % Notice that the note page is typeset at the beginning of the next slide
      \advance\beamer@slideinframe by-1\relax
      \beamer@atbeginnote%
      \beamer@notes%
      \ifx\beamer@noteitems\@empty\else
      \begin{enumerate}\itemsep=0pt\parskip=0pt%
        \beamer@noteitems%
      \end{enumerate}%
      \fi%
      \beamer@atendnote%
      \advance\beamer@slideinframe by 1\relax%
    }%
    \gdef\beamer@notesactions{}%
  }
}



\def\beamer@notesactions{}

\newcommand\AtBeginNote[1]{\long\def\beamer@atbeginnote{#1}}
\newcommand\AtEndNote[1]{\long\def\beamer@atendnote{#1}}

\AtBeginNote{}
\AtEndNote{}



%
% Note frame insert
%

\newcommand{\insertslideintonotes}[1]{{%
  \begin{pgfpicture}{0cm}{0cm}{#1\paperwidth}{#1\paperheight}
    \begin{pgflowlevelscope}{\pgftransformscale{#1}}%
      \color{normal text.bg}
      \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\paperwidth}{\paperheight}}
      \pgfusepath{fill,clip}
      \color{normal text.fg}
      {\pgftransformshift{\pgfpoint{\beamer@origlmargin}{\footheight}}\pgftext[left,bottom]{\copy\beamer@frameboxcopy}}
    \end{pgflowlevelscope}
  \end{pgfpicture}%
  }}


\mode
<article>
{
  \newcommand<>\note[2][]{}
}



\mode
<all>
